package com.example.chatroom.controller;

import com.example.chatroom.service.ChatService;
import com.example.chatroom.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author CookiesEason
 * 2018/12/17 14:45
 */
@Controller
public class Home {

    private UserInfoService userInfoService;

    private ChatService chatService;

    @Autowired
   public Home(UserInfoService userInfoService, ChatService chatService) {
       this.userInfoService = userInfoService;
       this.chatService = chatService;
   }

    @RequestMapping(value = {"index", "chatroom"})
    public String index(Model model, HttpServletRequest request) {
       Object object = request.getSession().getAttribute("nickname");
       Long userId = (Long) request.getSession().getAttribute("userId");
       if (object == null) {
           return "redirect:/login";
       }
       model.addAttribute("nickname",object);
        model.addAttribute("role", request.getSession().getAttribute("role"));
       model.addAttribute("historyMessages", chatService.findHistory(userId, request));
       return "chatroom";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String loginUser(Model model, String username, String password, HttpServletRequest request) {
       if (userInfoService.login(username, password, request)) {
            return "redirect:/index";
       } else {
           model.addAttribute("message","账号或密码错误");
           return "login";
       }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        userInfoService.logout(request);
        return "login";
    }

    @PostMapping("/register")
    public String registerUser(Model model,String username, String password, String repassword) {
        model.addAttribute("message", userInfoService.register(username, password, repassword));
        return "register";
    }

    @PostMapping(value = "/updateNickname")
    @ResponseBody
    public String updateNickname(String nickname,
                                            HttpServletRequest httpServletRequest) {
        return userInfoService.updateNickname(nickname, httpServletRequest);
    }

    @PostMapping(value = "/updatePassword")
    @ResponseBody
    public String updatePassword(String password, String newPassword,
                                 HttpServletRequest httpServletRequest) {
        return userInfoService.updatePassword(password, newPassword, httpServletRequest);
    }

    @GetMapping(value = "/forbidden")
    @ResponseBody
    public String forbidden(@RequestParam String nickname,
                             HttpServletRequest httpServletRequest) {
        return userInfoService.forbidden(nickname, httpServletRequest);
    }

}
